package com.wiscamp.ninechapters.panels.adapter.resources;

import com.wiscamp.ninechapters.panels.application.command.CreateApplicationCommand;
import com.wiscamp.ninechapters.panels.application.command.UpdateApplicationCommand;
import com.wiscamp.ninechapters.panels.application.services.ApplicationAppService;
import com.wiscamp.ninechapters.panels.application.vo.ApplicationVO;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 应用服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/ControlPanels")
@Tag(name = "控制面板-应用信息")
public class ApplicationController extends BaseController {

    private final ApplicationAppService applicationAppService;

    public ApplicationController(ApplicationAppService applicationAppService) {
        this.applicationAppService = applicationAppService;
    }

    @GetMapping("Applications/{appId}")
    @Operation(summary = "通过编号获取应用")
    @HandleResourceException
    public RestResponse<ApplicationVO> getApplication(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                      @PathVariable("appId") long appId) {
        return ResourceUtils.getById(request, response, appId, applicationAppService::getApplication);
    }


    @GetMapping("Applications/Code/{appCode}")
    @Operation(summary = "通过代码获取应用")
    @HandleResourceException
    public RestResponse<ApplicationVO> getApplicationByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @PathVariable("appCode") String appCode) {
        return ResourceUtils.getByText(request, response, appCode, applicationAppService::getApplicationByCode);
    }

    @GetMapping("Applications/Name/{appName}")
    @Operation(summary = "通过名称获取应用列表")
    @HandleResourceException
    public RestResponse<Collection<ApplicationVO>> getApplicationsByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                         @PathVariable("appName") String appName) {
        return ResourceUtils.getListByText(request, response, appName, applicationAppService::getApplicationsByName);
    }

    @GetMapping("Applications/Group/{appGroup}")
    @Operation(summary = "通过分组获取应用列表")
    @HandleResourceException
    public RestResponse<Collection<ApplicationVO>> getApplicationsByGroup(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                          @PathVariable("appGroup") String appGroup) {
        return ResourceUtils.getListByText(request, response, appGroup, applicationAppService::getApplicationsByGroup);
    }

    @GetMapping("Applications/Available")
    @Operation(summary = "获取可用应用列表")
    @HandleResourceException
    public RestResponse<Collection<ApplicationVO>> getAvailableApplications(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, applicationAppService::getAvailableApplications);
    }

    @PostMapping("Applications/Query")
    @Operation(summary = "分页获取应用列表")
    @HandleResourceException
    public RestResponse<Collection<ApplicationVO>> getApplicationsByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                          @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, applicationAppService::getApplicationsByQuery);
    }

    @PostMapping("Applications")
    @Operation(summary = "创建应用")
    @HandleResourceException
    public RestResponse<ApplicationVO> createApplication(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreateApplicationCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, applicationAppService::createApplication);
    }


    @PutMapping("Applications")
    @Operation(summary = "更新应用")
    @HandleResourceException
    public RestResponse<ApplicationVO> updateApplication(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody UpdateApplicationCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, applicationAppService::updateApplication);
    }

    @PutMapping("Applications/Actions/Delete")
    @Operation(summary = "通过编号删除应用")
    @HandleResourceException
    public RestResponse<Boolean> deleteApplication(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, applicationAppService::deleteApplication);
    }

    @PutMapping("Applications/Actions/Enable")
    @Operation(summary = "通过编号启用应用")
    @HandleResourceException
    public RestResponse<Boolean> enableApplication(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, applicationAppService::enableApplication);
    }

    @PutMapping("Applications/Actions/Disable")
    @Operation(summary = "通过编号禁用应用")
    @HandleResourceException
    public RestResponse<Boolean> disableApplication(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, applicationAppService::disableApplication);
    }
}
